texreg.ivcox <- 
function (l, file = NULL, single.row = FALSE, stars = c(0.001, 0.01, 0.05), custom.model.names = NULL, custom.coef.names = NULL, 
          custom.coef.map = NULL, custom.gof.names = NULL, custom.note = NULL, 
          digits = 2, leading.zero = TRUE, symbol = "\\cdot", 
          override.coef = 0, override.se = 0, override.pvalues = 0, 
          override.ci.low = 0, override.ci.up = 0, omit.coef = NULL, 
          reorder.coef = NULL, reorder.gof = NULL, ci.force = FALSE, 
          ci.force.level = 0.95, ci.test = 0, groups = NULL, custom.columns = NULL, 
          custom.col.pos = NULL, bold = 0, center = TRUE, caption = "Statistical models", 
          caption.above = FALSE, label = "table:coefficients", 
          booktabs = FALSE, dcolumn = FALSE, lyx = FALSE, sideways = FALSE, 
          longtable = FALSE, use.packages = TRUE, table = TRUE, no.margin = FALSE, 
          fontsize = NULL, scalebox = NULL, float.pos = "", dep.var = "", add.lines=NULL, add.lines.sep = FALSE, ...) 
{
  stars <- check.stars(stars)
  if (dcolumn == TRUE && bold > 0) {
    dcolumn <- FALSE
    msg <- paste("The dcolumn package and the bold argument cannot be used at", 
                 "the same time. Switching off dcolumn.")
    if (length(stars) > 1 || stars == TRUE) {
      warning(paste(msg, "You should also consider setting stars = 0."))
    }
    else {
      warning(msg)
    }
  }
  if (longtable == TRUE && sideways == TRUE) {
    sideways <- FALSE
    msg <- paste("The longtable package and sideways environment cannot be", 
                 "used at the same time. You may want to use the pdflscape package.", 
                 "Switching off sideways.")
    warning(msg)
  }
  if (longtable == TRUE && !(float.pos %in% c("", "l", 
                                              "c", "r"))) {
    float.pos <- ""
    msg <- paste("When the longtable environment is used, the float.pos", 
                 "argument can only take one of the \"l\", \"c\", \"r\", or \"\"", 
                 "(empty) values. Setting float.pos = \"\".")
    warning(msg)
  }
  if (longtable == TRUE && !is.null(scalebox)) {
    scalebox <- NULL
    warning(paste("longtable and scalebox are not compatible. Setting", 
                  "scalebox = NULL."))
  }
  models <- get.data(l, ...)
  gof.names <- get.gof(models)
  models <- override(models, override.coef, override.se, override.pvalues, 
                     override.ci.low, override.ci.up)
  models <- ciforce(models, ci.force = ci.force, ci.level = ci.force.level)
  models <- correctDuplicateCoefNames(models)
  gofs <- aggregate.matrix(models, gof.names, custom.gof.names, 
                           digits, returnobject = "gofs")
  m <- aggregate.matrix(models, gof.names, custom.gof.names, 
                        digits, returnobject = "m")
  decimal.matrix <- aggregate.matrix(models, gof.names, custom.gof.names, 
                                     digits, returnobject = "decimal.matrix")
  if (!is.null(custom.coef.map)) {
    m <- custommap(m, custom.coef.map)
  }
  else {
    m <- omitcoef(m, omit.coef)
    m <- customnames(m, custom.coef.names)
  }
  m <- rearrangeMatrix(m)
  m <- as.data.frame(m)
  m <- replaceSymbols(m)
  modnames <- modelnames(l, models, custom.model.names)
  m <- reorder(m, reorder.coef)
  gofs <- reorder(gofs, reorder.gof)
  decimal.matrix <- reorder(decimal.matrix, reorder.gof)
  lab.list <- c(rownames(m), gof.names)
  lab.length <- 0
  for (i in 1:length(lab.list)) {
    if (nchar(lab.list[i]) > lab.length) {
      lab.length <- nchar(lab.list[i])
    }
  }
  ci <- logical()
  for (i in 1:length(models)) {
    if (length(models[[i]]@se) == 0 && length(models[[i]]@ci.up) > 
        0) {
      ci[i] <- TRUE
    }
    else {
      ci[i] <- FALSE
    }
  }
  output.matrix <- outputmatrix(m, single.row, neginfstring = "\\multicolumn{1}{c}{$-\\infty$}", 
                                posinfstring = "\\multicolumn{1}{c}{$\\infty$}", 
                                leading.zero, digits, se.prefix = " \\; (", se.suffix = ")", 
                                star.prefix = "^{", star.suffix = "}", star.char = "*", 
                                stars, dcolumn = dcolumn, symbol, bold, bold.prefix = "\\mathbf{", 
                                bold.suffix = "}", ci = ci, semicolon = ";\\ ", 
                                ci.test = ci.test)
  output.matrix <- grouping(output.matrix, groups, indentation = "\\quad ", 
                            single.row = single.row, prefix = "", suffix = "")
  gof.matrix <- gofmatrix(gofs, decimal.matrix, dcolumn = TRUE, 
                          leading.zero, digits)
  
  # if there is add.lines argument provide, then add 
  if(!is.null(add.lines)){
    extras=matrix()[0]
    for(item in add.lines){
      if(length(item)!=(length(l)+1))
        stop('Need add.lines arg to be of the same size (1 per column)')
      extras = rbind(extras,item)
    }
    gof.matrix =rbind(extras, gof.matrix)
    
    if(add.lines.sep == FALSE){
      gof.names = c(extras[,1], gof.names)
    }
  }
  
  output.matrix <- rbind(output.matrix, gof.matrix)
  output.matrix <- customcolumns(output.matrix, custom.columns, 
                                 custom.col.pos, single.row = single.row, numcoef = nrow(m), 
                                 groups = groups, modelnames = FALSE)
  coltypes <- customcolumnnames(modnames, custom.columns, custom.col.pos, 
                                types = TRUE)
  modnames <- customcolumnnames(modnames, custom.columns, custom.col.pos, 
                                types = FALSE)
  coldef <- ""
  if (no.margin == FALSE) {
    margin.arg <- ""
  }
  else {
    margin.arg <- "@{}"
  }
  coefcount <- 0
  for (i in 1:length(modnames)) {
    if (coltypes[i] == "coef") {
      coefcount <- coefcount + 1
    }
    if (single.row == TRUE && coltypes[i] == "coef") {
      if (ci[coefcount] == FALSE) {
        separator <- ")"
      }
      else {
        separator <- "]"
      }
    }
    else {
      separator <- "."
    }
    if (coltypes[i] %in% c("coef", "customcol")) {
      alignmentletter <- "c"
    }
    else if (coltypes[i] == "coefnames") {
      alignmentletter <- "l"
    }
    if (dcolumn == FALSE) {
      coldef <- paste0(coldef, alignmentletter, margin.arg, 
                       " ")
    }
    else {
      if (coltypes[i] != "coef") {
        coldef <- paste0(coldef, alignmentletter, margin.arg, 
                         " ")
      }
      else {
        if (single.row == TRUE) {
          dl <- compute.width(output.matrix[, i], left = TRUE, 
                              single.row = TRUE, bracket = separator)
          dr <- compute.width(output.matrix[, i], left = FALSE, 
                              single.row = TRUE, bracket = separator)
        }
        else {
          dl <- compute.width(output.matrix[, i], left = TRUE, 
                              single.row = FALSE, bracket = separator)
          dr <- compute.width(output.matrix[, i], left = FALSE, 
                              single.row = FALSE, bracket = separator)
        }
        coldef <- paste0(coldef, "D{", separator, 
                         "}{", separator, "}{", dl, separator, 
                         dr, "}", margin.arg, " ")
      }
    }
  }
  string <- "\n"
  linesep <- if (lyx) 
    "\n\n"
  else "\n"
  if (use.packages == TRUE) {
    if (sideways == TRUE & table == TRUE) {
      string <- paste0(string, "\\usepackage{rotating}", 
                       linesep)
    }
    if (booktabs == TRUE) {
      string <- paste0(string, "\\usepackage{booktabs}", 
                       linesep)
    }
    if (dcolumn == TRUE) {
      string <- paste0(string, "\\usepackage{dcolumn}", 
                       linesep)
    }
    if (longtable == TRUE) {
      string <- paste0(string, "\\usepackage{longtable}", 
                       linesep)
    }
    if (dcolumn == TRUE || booktabs == TRUE || sideways == 
        TRUE || longtable == TRUE) {
      string <- paste0(string, linesep)
    }
  }
  if (longtable == TRUE) {
    if (center == TRUE) {
      string <- paste0(string, "\\begin{center}\n")
    }
    if (!is.null(fontsize)) {
      string <- paste0(string, "\\begin{", fontsize, 
                       "}", linesep)
    }
    if (float.pos == "") {
      string <- paste0(string, "\\begin{longtable}{", 
                       coldef, "}", linesep)
    }
    else {
      string <- paste0(string, "\\begin{longtable}[", 
                       float.pos, "]", linesep)
    }
  }
  else {
    if (table == TRUE) {
      if (sideways == TRUE) {
        t <- "sideways"
      }
      else {
        t <- ""
      }
      if (float.pos == "") {
        string <- paste0(string, "\\begin{", t, 
                         "table}", linesep)
      }
      else {
        string <- paste0(string, "\\begin{", t, 
                         "table}[", float.pos, "]", linesep)
      }
      if (caption.above == TRUE) {
        string <- paste0(string, "\\caption{", 
                         caption, "}", linesep)
      }
      if (center == TRUE) {
        string <- paste0(string, "\\begin{center}", 
                         linesep)
      }
      if (!is.null(fontsize)) {
        string <- paste0(string, "\\begin{", fontsize, 
                         "}", linesep)
      }
      if (!is.null(scalebox)) {
        string <- paste0(string, "\\scalebox{", 
                         scalebox, "}{\n")
      }
    }
    string <- paste0(string, "\\begin{tabular}{", coldef, 
                     "}", linesep)
    
    ########################################################################
    ## Additions
    string <- paste0(string, "\\hline \\hline", linesep)
    string <- paste0(string, "\\\\[-1.8ex] & \\multicolumn{", length(l), "}{c}{DEV: \\textit{", dep.var, "}} \\\\", linesep)
    string <- paste0(string, "\\\\[-1.8ex] \\cline{2-",length(modnames), "}", linesep)
  }
  tablehead <- "\\\\[-1.8ex]"
  if (booktabs == TRUE) {
    tablehead <- paste0(tablehead, "\\toprule", linesep)
  }
  else {
    ## tablehead <- paste0(tablehead, "\\hline", linesep)
  }
  tablehead <- paste0(tablehead, modnames[1])
  if (dcolumn == TRUE) {
    for (i in 2:length(modnames)) {
      if (coltypes[i] != "coef") {
        tablehead <- paste0(tablehead, " & ", modnames[i])
      }
      else {
        tablehead <- paste0(tablehead, " & \\multicolumn{1}{c}{", 
                            modnames[i], "}")
      }
    }
  }
  else {
    for (i in 2:length(modnames)) {
      tablehead <- paste0(tablehead, " & ", modnames[i])
    }
  }
  if (booktabs == TRUE) {
    tablehead <- paste0(tablehead, " \\\\", linesep, 
                        "\\midrule", linesep)
  }
  else {
    ## modified
    tablehead <- paste0(tablehead, " \\\\", linesep, 
                        "\\hline \\\\[-1.8ex]", linesep)
  }
  if (longtable == FALSE) {
    string <- paste0(string, tablehead)
  }
  if (is.null(stars)) {
    snote <- ""
  }
  else if (any(ci == FALSE)) {
    st <- sort(stars)
    if (length(unique(st)) != length(st)) {
      stop("Duplicate elements are not allowed in the stars argument.")
    }
    if (length(st) == 4) {
      snote <- paste0("$^{***}p<", st[1], "$, $^{**}p<", 
                      st[2], "$, $^*p<", st[3], "$, $^{", 
                      symbol, "}p<", st[4], "$")
    }
    else if (length(st) == 3) {
      snote <- paste0("$^{***}p<", st[1], "$, $^{**}p<", 
                      st[2], "$, $^*p<", st[3], "$")
    }
    else if (length(st) == 2) {
      snote <- paste0("$^{**}p<", st[1], "$, $^*p<", 
                      st[2], "$")
    }
    else if (length(st) == 1) {
      snote <- paste0("$^*p<", st[1], "$")
    }
    else {
      snote <- ""
    }
    if (is.numeric(ci.test) && !is.na(ci.test) && nchar(snote) > 
        0 && any(ci)) {
      snote <- paste(snote, "(or", ci.test, "outside the confidence interval).")
    }
    else if (is.numeric(ci.test) && !is.na(ci.test) && any(ci)) {
      snote <- paste("$^*$", ci.test, "outside the confidence interval")
    }
  }
  else if (is.numeric(ci.test) && !is.na(ci.test)) {
    snote <- paste("$^*$", ci.test, "outside the confidence interval")
  }
  else {
    snote <- ""
  }
  if (is.null(fontsize)) {
    notesize <- "scriptsize"
  }
  else if (fontsize == "tiny" || fontsize == "scriptsize" || 
           fontsize == "footnotesize" || fontsize == "small") {
    notesize <- "tiny"
  }
  else if (fontsize == "normalsize") {
    notesize <- "scriptsize"
  }
  else if (fontsize == "large") {
    notesize <- "footnotesize"
  }
  else if (fontsize == "Large") {
    notesize <- "small"
  }
  else if (fontsize == "LARGE") {
    notesize <- "normalsize"
  }
  else if (fontsize == "huge") {
    notesize <- "large"
  }
  else if (fontsize == "Huge") {
    notesize <- "Large"
  }
  if (is.null(custom.note)) {
    note <- paste0("\\multicolumn{", length(modnames), 
                   "}{l}{\\", notesize, "{Note:", snote, "}}")
  }
  else if (custom.note == "") {
    note <- ""
  }
  else {
    ## modified
    note <- paste0("\\multicolumn{", length(modnames), 
                   "}{l}{\\", notesize, "{", custom.note, 
                   "}}")
    note <- gsub("%stars", snote, note, perl = TRUE)
  }
  if (longtable == TRUE) {
    note <- paste0(note, "\\\\", linesep)
  }
  else {
    note <- paste0(note, linesep)
  }
  if (booktabs == TRUE) {
    bottomline <- paste0("\\bottomrule", linesep)
  }
  else {
    ## modified
    bottomline <- paste0("\\hline \\hline \\\\[-1.8ex]", linesep)
  }
  if (longtable == TRUE) {
    if (caption.above == TRUE) {
      string <- paste0(string, "\\caption{", caption, 
                       "}", linesep, "\\label{", label, 
                       "}\\\\", linesep, tablehead, "\\endfirsthead", 
                       linesep, tablehead, "\\endhead", linesep, 
                       bottomline, "\\endfoot", linesep, bottomline, 
                       note, "\\endlastfoot", linesep)
    }
    else {
      string <- paste0(string, tablehead, "\\endfirsthead", 
                       linesep, tablehead, "\\endhead", linesep, 
                       bottomline, "\\endfoot", linesep, bottomline, 
                       note, "\\caption{", caption, "}", 
                       linesep, "\\label{", label, "}", 
                       linesep, "\\endlastfoot", linesep)
    }
  }
  max.lengths <- numeric(length(output.matrix[1, ]))
  for (i in 1:length(output.matrix[1, ])) {
    max.length <- 0
    for (j in 1:length(output.matrix[, 1])) {
      if (nchar(output.matrix[j, i]) > max.length) {
        max.length <- nchar(output.matrix[j, i])
      }
    }
    max.lengths[i] <- max.length
  }
  for (i in 1:length(output.matrix[, 1])) {
    for (j in 1:length(output.matrix[1, ])) {
      nzero <- max.lengths[j] - nchar(output.matrix[i, 
                                                    j])
      zeros <- rep(" ", nzero)
      zeros <- paste(zeros, collapse = "")
      output.matrix[i, j] <- paste0(output.matrix[i, j], 
                                    zeros)
    }
  }
  
  ###############################################################
  ## modification
  tmp.line <- 1
  for (i in 1:(length(output.matrix[, 1]) - length(gof.matrix[,1]))) {
    for (j in 1:length(output.matrix[1, ])) {
      string <- paste0(string, output.matrix[i, j])
      if (j == length(output.matrix[1, ])) {
        if (tmp.line == 1){
          string <- paste0(string, " \\\\", linesep)
          tmp.line <- tmp.line + 1
        }
        else {
          string <- paste0(string, " \\\\ & & \\\\", linesep)
          tmp.line <- tmp.line - 1
        }
      }
      else {
        string <- paste0(string, " & ")
      }
    }
  }
  
  #id add.lines.sep = TRUE, then we want write the added lines separately from 
  #the gofs
  if(add.lines.sep == TRUE){
    if (booktabs == TRUE) {
      string <- paste0(string, "\\midrule", linesep)
    } else {
      string <- paste0(string, "\\hline", linesep)
    }
    
    for (i in (length(output.matrix[, 1]) - (length(gof.matrix[,1]) - 1)):
         (length(output.matrix[, 1]) - length(gof.names))){
      for (j in 1:length(output.matrix[1, ])) {
        string <- paste0(string, output.matrix[i, j])
        if (j == length(output.matrix[1, ])) {
          string <- paste0(string, " \\\\", linesep)
        } else {
          string <- paste0(string, " & ")
        }
      }
    }
  }
  
  if (length(gof.names) > 0) {
    if (booktabs == TRUE) {
      string <- paste0(string, "\\midrule", linesep)
    }
    else {
      string <- paste0(string, "\\hline", linesep)
    }
    for (i in (length(output.matrix[, 1]) - (length(gof.names) - 
                                             1)):(length(output.matrix[, 1]))) {
      for (j in 1:length(output.matrix[1, ])) {
        string <- paste0(string, output.matrix[i, j])
        if (j == length(output.matrix[1, ])) {
          string <- paste0(string, " \\\\", linesep)
        }
        else {
          string <- paste0(string, " & ")
        }
      }
    }
  }
  if (longtable == FALSE) {
    string <- paste0(string, bottomline)
    string <- paste0(string, note, "\\end{tabular}", 
                     linesep)
  }
  if (longtable == TRUE) {
    string <- paste0(string, "\\end{longtable}", linesep)
    if (!is.null(fontsize)) {
      string <- paste0(string, "\\end{", fontsize, 
                       "}", linesep)
    }
    if (center == TRUE) {
      string <- paste0(string, "\\end{center}", linesep)
    }
  }
  else if (table == TRUE) {
    if (!is.null(fontsize)) {
      string <- paste0(string, "\\end{", fontsize, 
                       "}", linesep)
    }
    if (!is.null(scalebox)) {
      string <- paste0(string, "}", linesep)
    }
    if (caption.above == FALSE) {
      string <- paste0(string, "\\caption{", caption, 
                       "}", linesep)
    }
    string <- paste0(string, "\\label{", label, "}", 
                     linesep)
    if (center == TRUE) {
      string <- paste0(string, "\\end{center}", linesep)
    }
    if (sideways == TRUE) {
      t <- "sideways"
    }
    else {
      t <- ""
    }
    string <- paste0(string, "\\end{", t, "table}", 
                     linesep)
  }
  if (is.null(file) || is.na(file)) {
    class(string) <- c("character", "texregTable")
    return(string)
  }
  else if (!is.character(file)) {
    stop("The 'file' argument must be a character string.")
  }
  else {
    sink(file)
    cat(string)
    sink()
    message(paste0("The table was written to the file '", 
                   file, "'.\n"))
  }
}